package io.bhex.sdk.data_manager;

import android.util.Log;

import com.bhex.kline.KlineKind;
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
import com.tencent.mmkv.MMKV;

import io.bhex.baselib.utils.DebugLog;
import io.bhex.baselib.utils.DevicesUtil;
import io.bhex.baselib.utils.SignUtils;
import io.bhex.sdk.BhexSdk;

/**
 * Created by BHEX.
 * User: gdy
 * Date: 2020/3/29
 * Time: 13:02
 */
public class MMKVManager {
    private MMKV mmkvPrivateData;
    private MMKV mmkvCacheConfig;
    private MMKV mmkv;

    private static volatile MMKVManager  _INSTANCE;

    private MMKVManager(){
        mmkv = MMKV.defaultMMKV();
        mmkvCacheConfig = MMKV.mmkvWithID("cache_data");
        String encryptKey = Hashing.sha256().hashString(DevicesUtil.getPackageName(BhexSdk.getContext()), Charsets.UTF_8).toString();
        mmkvPrivateData = MMKV.mmkvWithID("private_data",MMKV.SINGLE_PROCESS_MODE,encryptKey);
    }

    public static MMKVManager getInstance(){
        if(_INSTANCE==null){
            synchronized (MMKVManager.class){
                if(_INSTANCE==null){
                    _INSTANCE = new MMKVManager();
                }
            }
        }
        return _INSTANCE;
    }

    public MMKV mmkv() {
        return mmkv;
    }

    public void saveKind_T_Tag(String tag){
        if(tag.startsWith("kind_t")){
            KlineKind.KIND_T = tag;
            mmkv.encode(KlineKind.KIND_T_LABLE,tag);
        }
        if(tag.startsWith("kind_s")){
            KlineKind.KIND_SUB = tag;
            mmkv.encode(KlineKind.KIND_SUB_LABLE,tag);
        }

        if(tag.startsWith("kind_m")){
            KlineKind.KIND_MAIN = tag;
            mmkv.encode(KlineKind.KIND_MAIN_LABLE,tag);
        }
    }

    public void loadKind_T_Tag(){
        KlineKind.KIND_T =  mmkv.decodeString(KlineKind.KIND_T_LABLE,"kind_t_15m");
        //Log.d("MMKVManager==>:","KIND_T==="+KlineKind.KIND_T);
        KlineKind.KIND_MAIN =  mmkv.decodeString(KlineKind.KIND_MAIN_LABLE,"kind_m_ma");
        KlineKind.KIND_SUB =  mmkv.decodeString(KlineKind.KIND_SUB_LABLE,"kind_s_macd");
        //Log.d("MMKVManager==>:","KIND_SUB==="+KlineKind.KIND_SUB);
    }

    public MMKV mmkvCacheConfig() {
        return mmkvCacheConfig;
    }

    /**
     * 保存Book数量显示模式
     * @param isShowCumulativeQuantity
     */
    public void saveBookQuantityShowMode(boolean isShowCumulativeQuantity){
        mmkvCacheConfig.encode("isShowCumulativeQuantity",isShowCumulativeQuantity);
    }

    /**
     * 保存Book数量显示模式
     * @return boolean 默认累计数量显示默认 true
     */
    public boolean loadBookQuantityShowMode(){
        return mmkvCacheConfig.decodeBool("isShowCumulativeQuantity",true);
    }

    /**
     * 保存首页配置数据
     * @param key
     * @param cacheData
     */
    public void saveIndexMoudle(String key,String cacheData){
        mmkvCacheConfig.encode(key,cacheData);
    }

    /**
     * 加载首页配置数据
     * @param key
     * @return
     */
    public String loadIndexMoudle(String key){
        return mmkvCacheConfig.decodeString(key);
    }

    /**
     * 保存首页logoUrl
     * @param logoUrl
     */
    public void saveHomeLogoUrl(String logoUrl){
        mmkvCacheConfig.encode("home_logo_url",logoUrl);
    }

    /**
     * 加载首页logo
     * @return
     */
    public String loadHomeLogoUrl(){
        return mmkvCacheConfig.decodeString("home_logo_url");
    }

    /**
     * 保存备用域名列表
     * @param backupDomainList
     */
    public void saveBackupDomainList(String backupDomainList){
        mmkvPrivateData.encode("backup_domain_list",backupDomainList);
    }

    /**
     * 获取备用域名列表
     * @return
     */
    public String getBackupDomainList(){
        return mmkvPrivateData.decodeString("backup_domain_list");
    }

}
